package com.wn.class30.cotroller.weixin;

import com.wn.class30.entity.PayOrder;
import com.wn.class30.entity.User;
import com.wn.class30.entity.Vip;
import com.wn.class30.service.BusinessService;
import com.wn.class30.service.UserService;
import com.wn.class30.service.WXPayOrderService;
import com.wn.class30.tools.*;
import io.swagger.annotations.Api;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.weixin4j.WeixinException;
import org.weixin4j.model.pay.UnifiedOrder;
import org.weixin4j.model.pay.UnifiedOrderResult;
import org.weixin4j.model.pay.WCPay;
import org.weixin4j.model.pay.WXPay;
import org.weixin4j.spring.WeixinTemplate;
import org.weixin4j.util.SignUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping(value = "/weixin")
@Api(value = "微信jsap支付",tags = "微信jsap支付")
public class WXRealPayController {
    @Resource
 private UserService userService;
    @Resource
    private WeixinTemplate weixinTemplate;
    @Resource
    private UseridUtils useridUtils;
    @Resource
    private WXPayOrderService wxPayOrderService;
    @Resource
    private BusinessService businessService;
    @GetMapping("/xianxiabusinessPay")
    public Result WXRealPay(HttpServletRequest req,Double payMoney) throws Exception {
        UnifiedOrder unifiedOrder = new UnifiedOrder();
        unifiedOrder.setAppid(WXPayUtil.APPID);
        unifiedOrder.setMch_id(WXPayUtil.MCHID);
        unifiedOrder.setNonce_str(RandomStringUtils.random(16, "abcdefghijklmnopqrstuvwxyz"));
        unifiedOrder.setBody(WXPayUtil.BODY);
        String orderIdByTime = UserIp.getOrderIdByTime();
        unifiedOrder.setOut_trade_no(orderIdByTime);
        String s = MoneyUtils.yuanToFen(payMoney + "");
        System.out.println(payMoney);
        System.out.println(s);
        unifiedOrder.setTotal_fee(s);
        String ipAddress = UserIp.getIpAddress(req);
        System.out.println(ipAddress);
        unifiedOrder.setSpbill_create_ip(ipAddress);
        String openid = (String) req.getSession().getAttribute("openid");
        unifiedOrder.setOpenid(openid);
        unifiedOrder.setNotify_url(WXPayUtil.NOTIFYURL);
        unifiedOrder.setTrade_type("JSAPI");
        Map<String, String> stringStringMap = unifiedOrder.toMap();
        String sign = SignUtil.getSign(stringStringMap, WXPayUtil.PKEY);
        unifiedOrder.setSign(sign);
        //上面为设置微信支付参数
          //下面生成订单
        Integer businessId =(Integer) req.getSession().getAttribute("wxpaybusinessid");
        PayOrder payOrder = new PayOrder();
        payOrder.setBussinessId(businessId);
        payOrder.setPayMoney(payMoney);
        payOrder.setPayOrderState("0");
        payOrder.setPayUpdateTime(new Date());
        payOrder.setPayOrderNumber(orderIdByTime);
        payOrder.setBusinessName(businessService.selectBusinessById(businessId).getBusinessName());
        payOrder.setUserId(useridUtils.queryUseridByOptenid(req));
        wxPayOrderService.insertPayOrder(payOrder);
        //下单获取 prepay_id
        UnifiedOrderResult unifiedOrderResult = weixinTemplate.pay().payUnifiedOrder(unifiedOrder);
        String prepay_id = unifiedOrderResult.getPrepay_id();
        //获取 wcPay支付参数
        WCPay wcPay = new WCPay(WXPayUtil.APPID,prepay_id,WXPayUtil.PKEY);

        return new Result("200","sucess",wcPay,null);
    }
    @RequestMapping(value = "/queryVip")
    public Result queryPayVip(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid==null){
            return new Result("200","success",null,null);
        }else{
            Vip vip = userService.queryLvNameByUserid(userid);
            return new Result("200","success",vip,null);
        }
    }
}
